package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.vo.FloorGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WxHomeServiceImpl implements WxHomeService {

//brandList,floorGoodsList,hotGoodsList,newGoodsList,topicList

    @Autowired
    MarketAdMapper adMapper;

    @Autowired
    MarketCategoryMapper categoryMapper;

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketBrandMapper brandMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketTopicMapper topicMapper;

    @Override
    public Map<String, Object> index() {
        List<MarketAd> adList = queryAd();
        List<MarketCategory> categoryList = queryCategory();
        List<MarketCoupon> couponList = queryCoupon();
        List<MarketBrand> brandList = queryBrand();
        List<MarketGoods> hotGoodsList = queryHotGoods();
        List<MarketGoods> newGoodsList = queryNewGoods();
        List<MarketTopic> topicList = queryTopic();

        FloorGoodsVo floorGoodsVo1 = getFloorGoods(1005000, "居家");
        FloorGoodsVo floorGoodsVo2 = getFloorGoods(1005001, "餐厨");
        FloorGoodsVo floorGoodsVo3 = getFloorGoods(1005002, "饮食");
        FloorGoodsVo floorGoodsVo4 = getFloorGoods(1008000, "配件");
        List<FloorGoodsVo> floorGoodsVoList = new ArrayList<>();
        floorGoodsVoList.add(floorGoodsVo1);
        floorGoodsVoList.add(floorGoodsVo2);
        floorGoodsVoList.add(floorGoodsVo3);
        floorGoodsVoList.add(floorGoodsVo4);

        Map<String, Object> result = new HashMap<>();
        result.put("banner",adList);
        result.put("channel",categoryList);
        result.put("couponList",couponList);
        result.put("brandList",brandList);
        result.put("floorGoodsList",floorGoodsVoList);
        result.put("hotGoodsList",hotGoodsList);
        result.put("newGoodsList",newGoodsList);
        result.put("topicList",topicList);

        return result;
    }

    public List<MarketAd> queryAd() {
        MarketAdExample adExample = new MarketAdExample();
        adExample.or().andPositionEqualTo((byte) 1).andDeletedEqualTo(false);
        List<MarketAd> marketAds = adMapper.selectByExample(adExample);
        return marketAds;
    }

    public List<MarketCategory> queryCategory() {
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        categoryExample.or().andPidEqualTo(0).andDeletedEqualTo(false);
        List<MarketCategory> categoryList = categoryMapper.selectByExample(categoryExample);
        return categoryList;
    }

    public List<MarketCoupon> queryCoupon(){
        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.or().andTypeEqualTo((short)0).andDeletedEqualTo(false);
        List<MarketCoupon> couponList = couponMapper.selectByExample(couponExample);
        return couponList;
    }

    public List<MarketBrand> queryBrand(){
        MarketBrandExample brandExample = new MarketBrandExample();
        brandExample.or().andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = brandMapper.selectByExample(brandExample);

        List<MarketBrand> result = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            MarketBrand marketBrand = marketBrands.get(i);
            result.add(marketBrand);
        }
        return result;
    }

    public FloorGoodsVo getFloorGoods(int id, String name){
        FloorGoodsVo floorGoodsVo = new FloorGoodsVo();
        floorGoodsVo.setId(id);
        floorGoodsVo.setName(name);
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        categoryExample.or().andPidEqualTo(id);
        //获取所有的二级目录，商品的categoryId应该为二级目录的id
        List<MarketCategory> categoryListL2 = categoryMapper.selectByExample(categoryExample);
        List<MarketGoods> resultTotal = new ArrayList<>();
        for (MarketCategory category : categoryListL2) {
            Integer categoryId = category.getId();
            goodsExample.or().andCategoryIdEqualTo(categoryId);
            List<MarketGoods> marketGoods = goodsMapper.selectByExample(goodsExample);
            resultTotal.addAll(marketGoods);
        }

        List<MarketGoods> result = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            MarketGoods marketGoods = resultTotal.get(i);
            result.add(marketGoods);
        }

        floorGoodsVo.setGoodsList(result);
        return floorGoodsVo;
    }

    public List<MarketGoods> queryHotGoods(){
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        goodsExample.or().andIsHotEqualTo(true);
        List<MarketGoods> hotGoods = goodsMapper.selectByExample(goodsExample);

        List<MarketGoods> result = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            MarketGoods marketGoods = hotGoods.get(i);
            result.add(marketGoods);
        }
        return result;
    }

    public List<MarketGoods> queryNewGoods(){
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        goodsExample.or().andIsNewEqualTo(true);
        List<MarketGoods> newGoods = goodsMapper.selectByExample(goodsExample);

        List<MarketGoods> result = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            MarketGoods marketGoods = newGoods.get(i);
            result.add(marketGoods);
        }
        return result;
    }

    public List<MarketTopic> queryTopic(){
        MarketTopicExample topicExample = new MarketTopicExample();
        topicExample.or().andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = topicMapper.selectByExample(topicExample);

        List<MarketTopic> result = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            MarketTopic marketTopic = marketTopics.get(i);
            result.add(marketTopic);
        }
        return result;
    }
}
